import request from "@/utils/request";

// spu数据类型
export interface spuItemType {
  id?: number;
  spuName: string;
  description: string;
  category3Id: number;
  tmId: number | null;
  spuSaleAttrList: null | spuSaleAttrList;
  spuImageList: null | spuImageListType;
}

// 销售属性值数据类型
export interface spuSaleAttrValueItemType {
  id?: number;
  spuId?: number;
  isChecked?: number;
  baseSaleAttrId: number;
  saleAttrName: string;
  saleAttrValueName: string;
}
// 销售属性值列表类型
export type spuSaleAttrValueListType = spuSaleAttrValueItemType[];

// 销售属性数据类型
export interface spuSaleAttrItem {
  id?: number;
  spuId?: number;
  baseSaleAttrId: number;
  saleAttrName: string;
  spuSaleAttrValueList: spuSaleAttrValueListType;
  isEdit?: boolean;
}

// 销售属性列表类型
export type spuSaleAttrList = spuSaleAttrItem[];

// [id ?: number
//  spuId ?: number
//  baseSaleAttrId : 当前spu销售属性的id
//   saleAttrName : 当前的spu销售属性的名称
//   spuSaleAttrValueList: Array 当前的spu的销售属性值列表
//  [
//    id ?: number
//    isChecked ?: number
//    spuId ?: number
//    baseSaleAttrId : 当前spu销售属性的id
//    saleAttrName : 当前销售属性值所在的销售属性名称
//    saleAttrValueName : 当前spu销售属性值的名字] ]

// spu列表类型
export type spuListType = spuItemType[];

// 请求获取api 返回值类型
interface reqGetSpuListType {
  records: spuListType;
  total: number;
  size: number;
  current: number;
  searchCount: boolean;
  pages: number;
}

// 图片数据类型
export interface spuImageItem {
  id?: number;
  spuId?: number;
  imgName: string;
  imgUrl: string;
}

// 图片列表类型
export type spuImageListType = spuImageItem[]; // spuImageList : Array spu图片列表 [ imgName : 图片的名称 imgUrl : 图片的地址 ] }

// 请求获取spu分页列表
export const reqGetSpuList = (
  page: number,
  limit: number,
  category3Id: number
) => {
  return request.get<null, reqGetSpuListType>(
    `/admin/product/${page}/${limit}?category3Id=${category3Id}`
  );
};

// 销售属性类型
export interface salesAttrItemType {
  id: number;
  name: string;
}

// 销售属性返回值类型
export type salesAttrListType = salesAttrItemType[];

// 请求所有的销售属性
export const reqGetAllSalesAttr = () => {
  return request.get<null, salesAttrListType>(
    `/admin/product/baseSaleAttrList`
  );
};

// 请求新增spu
export const reqAddSpuInfo = (spuInfo: spuItemType) => {
  return request.post<null, null>(`/admin/product/saveSpuInfo`, spuInfo);
  //  返回spuList 页
};

// 请求修改spu
export const reqUpdataSpuInfo = (spuInfo: spuItemType) => {
  return request.post<null, null>(`/admin/product/updateSpuInfo`, spuInfo);
};

// 请求根据spuId 获取销售属性列表
export const reqGetSalesAttrListBySpuId = (spuId: number) => {
  return request.get<null, spuSaleAttrList>(
    `/admin/product/spuSaleAttrList/${spuId}`
  );
};

// 请求根据spuId 获取image列表
export const reqGetImageListBySpuId = (spuId: number) => {
  return request.get<null, spuImageListType>(
    `/admin/product/spuImageList/${spuId}`
  );
};
